package com.ruoyi.system.controller;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Bid;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.service.IBidService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Task;
import com.ruoyi.system.service.ITaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 任务Controller
 *
 * @author 李演
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ITypeService iTypeService;
    @Autowired
    private IBidService bidService;
    /**
     * 查询任务列表
     */
    @RequiresPermissions("system:task:list")
    @GetMapping("/list")
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        for (Task t : list) {
            if (t.getEmployeeId()!=null){
                SysUser sysUser = iSysUserService.selectUserById(t.getEmployeeId());
                t.setEmployeeName(sysUser.getUserName());
            }
            if (t.getEmployerId()!=null){
                SysUser sysUser = iSysUserService.selectUserById(t.getEmployerId());
                t.setEmployerName(sysUser.getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 任务大厅
     */
    @RequiresPermissions("system:task:list")
    @GetMapping("/taskHall")
    public TableDataInfo taskHall(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        list = list.stream().filter(tempTask->tempTask.getEmployeeId()==null).collect(Collectors.toList());
        for (Task t : list) {
            if (t.getEmployeeId()!=null){
                SysUser sysUser = iSysUserService.selectUserById(t.getEmployeeId());
                t.setEmployeeName(sysUser.getUserName());
            }
            if (t.getEmployerId()!=null){
                SysUser sysUser = iSysUserService.selectUserById(t.getEmployerId());
                t.setEmployerName(sysUser.getUserName());
            }
        }
        return getDataTable(list);
    }
    //竞标查询
    @RequiresPermissions("system:task:list")
    @GetMapping("/listBidTask")
    public TableDataInfo listBidTask(Task task)
    {
        startPage();
        List<Task> list = taskService.selectBidList(task);
        for (Task t : list) {
            if(t.getCategoryId()!=null){
                Type type = iTypeService.selectTypeById(t.getCategoryId());
                t.setName(type.getName());
            }
            if (t.getEmployerId()!=null){
                SysUser sysUser = iSysUserService.selectUserById(t.getEmployerId());
                t.setEmployerName(sysUser.getUserName());
            }
        }
        return getDataTable(list);
    }
    @GetMapping("/getPriceSum/{id}")
    public Long getPriceSum(@PathVariable("id") Long employeeId)
    {
        return taskService.priceSum(employeeId)==null?0:taskService.priceSum(employeeId);
    }

    /**
     * 导出任务列表
     */
    //@RequiresPermissions("system:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
  //  @RequiresPermissions("system:task:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Task task = taskService.selectTaskById(id);
        if (task.getEmployeeId()!=null){
            SysUser sysUser = iSysUserService.selectUserById(task.getEmployeeId());
            task.setEmployeeName(sysUser.getUserName());
        }
        if (task.getEmployerId()!=null){
            SysUser sysUser = iSysUserService.selectUserById(task.getEmployerId());
            task.setEmployerName(sysUser.getUserName());
        }
        return success(task);
    }

    /**
     * 新增任务
     */
//    @RequiresPermissions("system:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task)
    {
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务
     */
//    @RequiresPermissions("system:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }
    //雇主接受投标
    @Transactional
    @PutMapping("/acceptBidding")
    public AjaxResult acceptBidding(@RequestBody Task task)
    {
        Bid bid = new Bid();
        bid.setId(task.getBidId());
        bid.setState(1L);
        bidService.updateBid(bid);
        return toAjax(taskService.updateTask(task));
    }

    @PutMapping("/refuseBidding")
    public AjaxResult refuseBidding(@RequestBody Task task)
    {
        Bid bid = new Bid();
        bid.setId(task.getBidId());
        bid.setState(2L);
        return toAjax(bidService.updateBid(bid));
    }

    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmCompletion")
    public AjaxResult confirmCompletion(@RequestBody Task task)
    {
        task.setTransactionPrice(task.getExpectedPrice());
        return toAjax(taskService.updateTask(task));
    }

    @Log(title = "投标", businessType = BusinessType.UPDATE)
    @PutMapping("/tender")
    public AjaxResult tender(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskService.deleteTaskByIds(ids));
    }
}
